package com.bookseek.xreader.views.helper;

import android.content.Context;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.ImageView;

public class SearchViewHelper {
    private Context context;
    private EditText searchEditText;
    private ImageView clearButton;
    private OnSearchListener searchListener;

    public interface OnSearchListener {
        void onSearch(String query);
        void onTextChanged(String text);
    }

    public SearchViewHelper(Context context, EditText editText, ImageView clearBtn) {
        this.context = context;
        this.searchEditText = editText;
        this.clearButton = clearBtn;
        setupSearchView();
    }

    private void setupSearchView() {
        // 文本变化监听
        searchEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {}

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 显示/隐藏清除按钮
                clearButton.setVisibility(s.length() > 0 ? View.VISIBLE : View.GONE);

                // 回调文本变化
                if (searchListener != null) {
                    searchListener.onTextChanged(s.toString());
                }
            }

            @Override
            public void afterTextChanged(Editable s) {}
        });

        // 清除按钮点击事件
        clearButton.setOnClickListener(v -> {
            searchEditText.setText("");
            hideKeyboard();
        });

        // 软键盘搜索按钮监听
        searchEditText.setOnEditorActionListener((v, actionId, event) -> {
            if (actionId == EditorInfo.IME_ACTION_SEARCH) {
                performSearch();
                return true;
            }
            return false;
        });
    }

    // 执行搜索
    public void performSearch() {
        String query = searchEditText.getText().toString().trim();
        if (searchListener != null) {
            searchListener.onSearch(query);
        }
        hideKeyboard();
    }

    // 隐藏软键盘
    private void hideKeyboard() {
        InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
        imm.hideSoftInputFromWindow(searchEditText.getWindowToken(), 0);
    }

    // 设置搜索监听器
    public void setOnSearchListener(OnSearchListener listener) {
        this.searchListener = listener;
    }

    // 获取当前搜索文本
    public String getSearchText() {
        return searchEditText.getText().toString().trim();
    }

    // 设置搜索文本
    public void setSearchText(String text) {
        searchEditText.setText(text);
        if (text != null && !text.isEmpty()) {
            searchEditText.setSelection(text.length());
            clearButton.setVisibility(View.VISIBLE);
        }
    }

}
